import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

data = pd.read_excel('./data/house_price.xlsx')

plt.rcParams['font.sans-serif'] = 'SimHei'  # 设置字体为SimHei
plt.rcParams['axes.unicode_minus'] = False  # 解决负号“-”显示异常
plt.title('房屋面积-价格图')

plt.xlabel('面积（㎡）')
plt.ylabel('价格（w）')

x = data.loc[:, '面积（㎡）'].values
y = data.loc[:, '价格（w）'].values

# 根据公式计算w和b
x_bar = np.mean(x)

molecule = np.sum(y * (x - x_bar))
denominator = np.sum(x * x) - (np.sum(x)) * (np.sum(x)) / np.size(x)
w = molecule / denominator
b = np.mean(y - x * w)

# 回归预测
y_mat = w * x +b

# 绘图
plt.scatter(x, y)
plt.plot(x, y_mat)
plt.show()